package com.winchain.chainbill.receivables.biz.impt.cmd;

import com.shell.bee.acct.AcctException;
import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICommand;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.receivables.base.po.ArOperation;
import com.winchain.chainbill.receivables.base.po.ArRecord;
import com.winchain.chainbill.trade.TradeException;
import com.winchain.chainbill.trade.service.BillAcctService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ReceivablesTranCmd implements ICommand<ArRecord> {
	@Autowired
	private TradeService tradeService;
	@Autowired
	private BillAcctService billAcctService;

	public boolean execute(CommandContext<ArRecord> context) throws Exception {
		boolean timeout = ((Boolean) context.getValue("Timeout")).booleanValue();
		if (!timeout) {
			AcctTxRec tran = getAcctTxRec(context);
			this.tradeService.fire(tran);

			context.setValue("txSn", tran.getTxSn());
		}
		return false;
	}

	private AcctTxRec getAcctTxRec(CommandContext<ArRecord> context) {
		ArRecord record = (ArRecord) context.getSource();

		String bizType = ((ArOperation) context.getValue("Operation")).getBizType();
		String finId = "";
		CusRec fin = (CusRec) context.getValue("Fin");
		if (fin != null) {
			finId = fin.getCusId();
		}
		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());

		BigDecimal amount = record.getCreditMoney();
		BigDecimal cmo = record.getCreditMoneyOrig();
		String abs = "������������";

		String tranEntId = finId;
		String cpId = record.getSellerId();
		if (cmo != null) {
			abs = "������������";
			if (amount.compareTo(cmo) < 0) {
				amount = cmo.subtract(amount);

				tranEntId = record.getSellerId();
				cpId = finId;
			} else {
				amount = amount.subtract(cmo);
			}
		}
		if (amount.compareTo(new BigDecimal(0)) <= 0) {
			throw new TradeException("AcctTxRec amount<=0 !");
		}
		String tranAcctNo = getAcctBas(tranEntId, finId).getAcctNo();
		txRec.setTxAcctNo(tranAcctNo);
		txRec.setTxAmt(record.getCreditMoney());
		txRec.setTxDt(DateUtil.getSystemDate());
		txRec.setTxTs(DateUtil.getSystemDateTime());
		txRec.setTxAbs(abs);
		txRec.setTxType(bizType);

		txRec.setTxSts("I");

		AcctBas acct = getAcctBas(cpId, finId);
		txRec.setCpAcctNo(acct.getAcctNo());
		txRec.setCpAcctNm(acct.getAcctNm());
		txRec.setCpBankNm(acct.getFiNm());
		txRec.setCurrCode("QUOTA");
		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(record.getArId());
		txRec.setDataScLvl("B");
		return txRec;
	}

	private AcctBas getAcctBas(String custId, String fiNo) {
		String acctType = "1";
		AcctBas acct = this.billAcctService.getAcctBas(custId, acctType, null);
		if (acct == null) {
			throw new AcctException(custId + "-" + acctType + "����������");
		}
		return acct;
	}
}
